package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.aletto.general_store.R;
import com.aletto.general_store.configs.IBridgeData;
import com.aletto.general_store.ui.fragments.FavoritesGoodsFragment;
import com.aletto.general_store.ui.fragments.FavoritesStoreFragment;
import com.aletto.general_store.ui.popup.ConfirmFavoritesPopup;
import com.aletto.general_store.utils.XHelper;
import com.lxj.xpopup.XPopup;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 收藏
 */
public class FavoritesActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    private MagicIndicator magicIndicator;

    private final List<Fragment> mFragmentList = new ArrayList<>(
            Arrays.asList(new FavoritesGoodsFragment(), new FavoritesStoreFragment()));

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorites);
        XHelper.initTitle(this, R.string.favorites);

        // 页面
        viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(new FragmentStateAdapter(this) {

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return mFragmentList.size();
            }

        });

        // 标签
        magicIndicator = findViewById(R.id.magic_indicator);
        magicIndicator.setNavigator(getCommonNavigator());

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });


        // 清除
        findViewById(R.id.tv_clear_history).setOnClickListener(v -> {

            IBridgeData bridge = (IBridgeData) mFragmentList.get(viewPager.getCurrentItem());
            if (bridge.hasData()) {
                ConfirmFavoritesPopup popup = new ConfirmFavoritesPopup(v.getContext());
                popup.setType(viewPager.getCurrentItem());
                new XPopup.Builder(v.getContext()).asCustom(popup).show();
            }

        });
    }


    @NonNull
    private CommonNavigator getCommonNavigator() {
        String[] tabs_favorites = getResources().getStringArray(R.array.tabs_favorites);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return tabs_favorites.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setText(tabs_favorites[index]);
                colorTransitionPagerTitleView.setGravity(Gravity.CENTER);
                colorTransitionPagerTitleView.setNormalColor(Color.parseColor("#848E9C"));
                colorTransitionPagerTitleView.setSelectedColor(Color.parseColor("#ED5E62"));
                colorTransitionPagerTitleView.setOnClickListener(v -> viewPager.setCurrentItem(index));
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                linePagerIndicator.setColors(Color.parseColor("#ED5E62"));
                return linePagerIndicator;
            }
        });

        return commonNavigator;
    }


}
